package cn.b.sky.i18n.service.impl;

import cn.b.sky.common.constants.SystemConstant;
import cn.b.sky.i18n.dao.LocaleResourceDao;
import cn.b.sky.i18n.dao.LocaleResourceTLDao;
import cn.b.sky.i18n.enums.LocaleResourceEnum;
import cn.b.sky.i18n.model.LocaleResource;
import cn.b.sky.i18n.model.LocaleResourceTL;
import cn.b.sky.i18n.model.SystemLanguageInfo;
import cn.b.sky.i18n.model.condition.LocaleResourceCondition;
import cn.b.sky.i18n.service.I18nService;
import cn.b.sky.i18n.service.LocaleResourceService;
import cn.b.sky.i18n.utils.LocaleResourceBundle;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("localeResourceService")
public class LocaleResourceServiceImpl implements LocaleResourceService {

  private static final Logger log = LoggerFactory.getLogger(LocaleResourceServiceImpl.class);

  @Resource
  private LocaleResourceDao localeResourceDao;

  @Resource
  private LocaleResourceTLDao localeResourceTLDao;
  
  @Resource
  private I18nService i18nService;

  @Override
  public List<LocaleResource> selectLocaleResourceByCondition(LocaleResourceCondition condition) {
    return localeResourceDao.selectLocaleResourceByCondition(condition);
  }

  @Override
  public int selectLocaleResourceCountByCondition(LocaleResourceCondition condition) {
    return localeResourceDao.selectLocaleResourceCountByCondition(condition);
  }

  @Override
  public void insertLocaleResource(LocaleResource localeResource, String languageCode) {
    localeResourceDao.insertLocaleResource(localeResource);
  }

  @Override
  public LocaleResource selectLocaleResourceByKey(String resourceSet, String resourceKey, String siteCode) {
    return localeResourceDao.selectLocaleResourceByKey(resourceSet, resourceKey, siteCode);
  }

  @Override
  public void updateLocaleResource(LocaleResource localeResource) {
    localeResourceDao.updateLocaleResource(localeResource);
  }

  @Override
  public int checkLocaleResourceByKey(LocaleResource localeResource) {
    return localeResourceDao.checkLocaleResourceByKey(localeResource);
  }

  @Override
  public String deleteLocaleResourceByKey(String resourceSet, String resourceKey, String siteCode) {
    String message = null;
    try {
      LocaleResourceTL tl = new LocaleResourceTL();
      tl.setSiteCode(siteCode);
      tl.setResourceSet(resourceSet);
      tl.setResourceKey(resourceKey);
      localeResourceTLDao.deleteLocaleResourceTL(tl);
      localeResourceDao.deleteLocaleResourceByKey(resourceSet, resourceKey, siteCode);
    } catch (Exception e) {
       message = SystemConstant.DWZ_CODE_ERROR_MESSAGE;
    }
    return message;
  }

  @Override
  public void flushResouceCache(String siteCode) {
    if(StringUtils.isBlank(siteCode)){
      return;
    }
    List<SystemLanguageInfo> list = i18nService.listLanguage();
    for (SystemLanguageInfo language : list) {
      LocaleResourceBundle bundle = LocaleResourceBundle.getBundle(siteCode,
          LocaleResourceEnum.COMMONRESOURCES.getValue(), language.getLanguageCode());
      if(bundle !=null){
        i18nService.flushLocaleResourceBundleByLanguage(siteCode, language.getLanguageCode());
      }
    }  
  }

}
